﻿#include "financestandarditemmodel.h"

FinanceStandardItemModel::FinanceStandardItemModel(QObject *parent) : QStandardItemModel(parent)
{
    header<<"单号"
         <<"时间"
        <<"类型"
       <<"商品ID"
      <<"型号"
     <<"名称"
    <<"单位"
    <<"数量"
    <<"单价"
    <<"金额"
    <<"应开发票"
    <<"客户名称"
    <<"联系人"
    <<"电话"
    <<"本次金额";
}

bool FinanceStandardItemModel::setData(const QModelIndex &index, const QVariant &value, int role){
    if(index.column() != header.size()-1){
        return false;
    }

    double curValue = item(index.row(), 10)->text().toDouble();
    double insertValue = value.toDouble();

    if(curValue >= 0){
        insertValue = (insertValue>=0&&insertValue<=curValue)?insertValue:0.0;
    }else{
        insertValue = (insertValue<=0&&insertValue>=curValue)?insertValue:0.0;
    }

    DingdanDetail *detail = details[index.row()];
    detail->financeMake = insertValue;
    QStandardItemModel::setData(index, insertValue, role);
    refresh();
    return true;
}

QVariant FinanceStandardItemModel::data(const QModelIndex &index, int role) const{
    QVariant value = QStandardItemModel::data(index, role);
    //设置内容居中
    if(role==Qt::TextAlignmentRole){
        return  Qt::AlignCenter;
    }

    //设置内容颜色
    if (role == Qt::TextColorRole && (index.column() == header.size()-1))
        return QVariant::fromValue(QColor(Qt::blue));
    return value;
}

void FinanceStandardItemModel::setWidgetType(Dingdan::FinanceWidgetType widgetType){
    this->widgetType = widgetType;

    if(widgetType == Dingdan::TicketOut){
        header[10] = "应开发票";
        header[11] = "客户名称";
        header[14] = "本次开票";
    }else if(widgetType == Dingdan::MoneyIn){
        header[10] = "应收货款";
        header[11] = "客户名称";
        header[14] = "本次收款";
    }else if(widgetType == Dingdan::TicketIn){
        header[10] = "应收发票";
        header[11] = "供应商名称";
        header[14] = "本次收票";
    }else{
        header[10] = "应付货款";
        header[11] = "供应商名称";
        header[14] = "本次付款";
    }

    setHorizontalHeaderLabels(header);
}

void FinanceStandardItemModel::reset(){
    clear();
    details.clear();
    setHorizontalHeaderLabels(header);
}

void FinanceStandardItemModel::refresh(){
    emit updateTotal(totalFinanceNeed());
}

double FinanceStandardItemModel::totalFinanceNeed(){
    double sum = 0;

    for(int i=0;i<rowCount();i++){
        double curValue = item(i, 10)->text().toDouble();
        sum += curValue;
    }

    return sum;
}

double FinanceStandardItemModel::totalFinanceMake(){
    double sum = 0;
    for(int i=0;i<rowCount();i++){
        double curValue = item(i, 14)->text().toDouble();
        sum += curValue;
    }

    return sum;
}

QList<DingdanDetail*> FinanceStandardItemModel::validDingdanDetail(){
    QList<DingdanDetail*> validDetails;
    for(DingdanDetail *detail:details){
        if(detail->financeMake != 0.0){
            validDetails.append(detail);
        }
    }

    return validDetails;
}

void FinanceStandardItemModel::financeAllocate(double totalFinance){
    for(int i=0;i<rowCount();i++){
        item(i, 14)->setText("0");
    }

    for(int i=0;i<rowCount();i++){
        double financeValue = item(i, 10)->text().toDouble();
        DingdanDetail *detail = details[i];
        if(totalFinance <= 0){
            if(financeValue<0){
                totalFinance -= financeValue;
                item(i, 14)->setText(QString("%1").arg(financeValue));
                detail->financeMake = financeValue;
            }else{
                break;
            }
        }else if(totalFinance>=financeValue){
            item(i, 14)->setText(QString("%1").arg(financeValue));
            detail->financeMake = financeValue;
            totalFinance -= financeValue;
        }else{
            item(i, 14)->setText(QString("%1").arg(totalFinance));
            detail->financeMake = totalFinance;
            totalFinance = 0;
            break;
        }
    }
}

void FinanceStandardItemModel::insertDingdanDetail(DingdanAndDetail &detail){
    QList<QStandardItem*> rowData;

    Product *product = detail.detail.getProduct();
    UserRelation *relation = detail.dingdan.getUserRelation();

    rowData.append(new QStandardItem(detail.dingdan.dingdanId));
    rowData.append(new QStandardItem(detail.dingdan.date));
    rowData.append(new QStandardItem(detail.dingdan.getTypeStr()));
    rowData.append(new QStandardItem(product==nullptr?"":product->productId));
    rowData.append(new QStandardItem(product==nullptr?"":product->type));
    rowData.append(new QStandardItem(product==nullptr?"":product->name));
    rowData.append(new QStandardItem(product==nullptr?"":product->unit));
    rowData.append(new QStandardItem(QString("%1").arg(detail.detail.count)));
    rowData.append(new QStandardItem(QString::number(detail.detail.price, 'f', 2)));
    rowData.append(new QStandardItem(QString::number(detail.detail.count*detail.detail.price, 'f', 2)));
    double value=0.0;
    switch (widgetType) {
    case Dingdan::TicketOut:
        value = detail.detail.ticketOut;
        break;
    case Dingdan::MoneyIn:
        value = detail.detail.moneyIn;
        break;
    case Dingdan::TicketIn:
        value = detail.detail.ticketIn;
        break;
    case Dingdan::MoneyOut:
        value = detail.detail.moneyOut;
        break;
    }
    rowData.append(new QStandardItem(QString("%1").arg(value)));
    rowData.append(new QStandardItem(relation==nullptr?"":relation->nickName));
    rowData.append(new QStandardItem(relation==nullptr?"":relation->contact));
    rowData.append(new QStandardItem(relation==nullptr?"":relation->tel));
    rowData.append(new QStandardItem("0"));

    appendRow(rowData);
    details.append(&detail.detail);
}

void FinanceStandardItemModel::insertDingdanDetail(QList<DingdanAndDetail*> details){
    for(DingdanAndDetail *detail:details){
        insertDingdanDetail(*detail);
    }
    refresh();
}
